﻿using UnityEngine;
using System.Collections;

public class backgroundfade : MonoBehaviour {
	
	public float aValue = 0.0f;
    public float aTime;
    private Transform[] transforms;
    public bool mountain = false;
    int j;
	void Start () 
    {
       transforms  = new Transform[transform.childCount+1];

       if (mountain)
       {
           j = 1;
       }
       else
       {
           transforms[0] = transform;
           j = 1;
       }
        for (int i = 0; i < transform.childCount; i++)
        {
            transforms[j] = transform.GetChild(i);
            j++;
        }
        StartCoroutine(FadeTo());
  
    }

	IEnumerator FadeTo()
	{
        //print(transforms.Length);
        foreach (Transform child in transforms)
        {
            float alpha = child.renderer.material.color.a;
            for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime)
            {
                Color newColor = new Color(1, 1, 1, Mathf.Lerp(alpha, aValue, t));
                child.renderer.material.color = newColor;
                yield return null;
            }
        }
        System.Array.Reverse(transforms);
        if(aValue == 1)
        {
            aValue = 0;
        }
        else
        {
            aValue = 1;
        }
        StartCoroutine(FadeTo());
    }
}
